﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;


namespace H2.NET.Web.Pages.MDC
{
    public partial class Determination_D_plant : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }

                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }
 
        private string CustomerCode
        {
            get
            {
                return ViewState["CustomerCode"] == null ? "" : (string)ViewState["CustomerCode"];

            }
            set { ViewState["CustomerCode"] = value; }
        }

        private string CustomerName
        {
            get
            {
                return ViewState["CustomerName"] == null ? "" : (string)ViewState["CustomerName"];

            }
            set { ViewState["CustomerName"] = value; }
        }

        public string ProductionLine
        {
            get
            {
                return ViewState["ProductionLine"] == null ? "" : (string)ViewState["ProductionLine"];

            }
            set { ViewState["ProductionLine"] = value; }
        }

        public string Color
        {
            get
            {
                return ViewState["Color"] == null ? "" : (string)ViewState["Color"];

            }
            set { ViewState["Color"] = value; }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }

        public bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }
        #endregion
        #region 事件 Page_Load(页面加载)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Status.IsUsed)
            {
                return;
            }

            bool flg = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("00", "Sales Organization<br/>VKORG)", BoundColumnType2.TextBox, 150, Align.Left, false, "Sales_Organization", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("01", "Customer Number 1<br/>KUNNR", BoundColumnType2.TextBox, 150, Align.Left, flg, "Customer_Number_1", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("02", "Function", BoundColumnType2.TextBox, 60, Align.Left, flg, "Function_1", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("03", "Postal Code<br/>PSTLZ", BoundColumnType2.TextBox, 100, Align.Left, flg, "Postal_Code", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("04", "Material Number<br/>MATNR", BoundColumnType2.TextBox, 150, Align.Left, flg, "Material_Number", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("05", "Material Description", BoundColumnType2.TextBox, 150, Align.Left, flg, "Material_Description", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("06", "Brand<br/>ZZBRND", BoundColumnType2.TextBox, 60, Align.Left, flg, "Brand", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("07", "Brand Description<br/>KUNNR", BoundColumnType2.TextBox, 150, Align.Left, flg, "Brand_Description", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("08", "Sub-brand<br/>ZZSUBR", BoundColumnType2.TextBox, 80, Align.Left, flg, "Sub_brand", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("09", "Sub-brand Description", BoundColumnType2.TextBox, 100, Align.Left, flg, "Sub_brand_Description", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("10", "Technology<br/>ZTECHNO", BoundColumnType2.TextBox, 80, Align.Left, flg, "Technology", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("11", "Transportation zone", BoundColumnType2.TextBox, 130, Align.Left, flg, "Transportation_zone", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("12", "Volume rebate group(category)<br/>BONUS", BoundColumnType2.TextBox, 200, Align.Left, flg, "Volume_rebate_group", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("13", "Material Group<br/>MATKL", BoundColumnType2.TextBox, 130, Align.Left, flg, "Material_Group", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("14", "Plant<br/>WERKS", BoundColumnType2.TextBox, 80, Align.Left, flg, "Plant", InputType.None, null));
            gv_Determination_1.DynamicColumn.Add(new BoundColumn2("15", "Old Material", BoundColumnType2.TextBox, 80, Align.Left, flg, "Old_Material", InputType.None, null));
            gv_Determination_1.Width = 1700;
            SetEnable(flg, Add, Delete);
            if (!Page.IsPostBack)
            {
                InitControls();
            }
        }
        #endregion
        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            foreach (mdc_spm_Determination_1 ccc in (List<mdc_spm_Determination_1>)gv_Determination_1.DataSource)
            {
                if (string.IsNullOrEmpty(ccc.Brand) && string.IsNullOrEmpty(ccc.Brand_Description) && string.IsNullOrEmpty(ccc.Customer_Number_1) && string.IsNullOrEmpty(ccc.Function_1) && string.IsNullOrEmpty(ccc.Material_Description) && string.IsNullOrEmpty(ccc.Material_Group) && string.IsNullOrEmpty(ccc.Material_Number) && string.IsNullOrEmpty(ccc.Old_Material) && string.IsNullOrEmpty(ccc.Plant) && string.IsNullOrEmpty(ccc.Postal_Code) && string.IsNullOrEmpty(ccc.Sub_brand) && string.IsNullOrEmpty(ccc.Sub_brand_Description) && string.IsNullOrEmpty(ccc.Technology) && string.IsNullOrEmpty(ccc.Transportation_zone) && string.IsNullOrEmpty(ccc.Volume_rebate_group))
                {
                    continue;
                }
                ccc.Type = "1";
                ccc.Process = TaskAPI.Process;
                ccc.Incident = TaskAPI.Incident;
                ccc.UserName = UserAPI.UserName;
                ccc.Time = System.DateTime.Now;
                ccc.Status = 1;
                Storage.Save(ccc);
                //string s = ccc.ProductionLine + ccc.Color;
                //TaskAPI["ProductionLine"] = ccc.ProductionLine;
                //TaskAPI["Color"] = ccc.Color;
            }
        }
        #endregion
        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            List<mdc_spm_Determination_1> sefi_1_s = (List<mdc_spm_Determination_1>)gv_Determination_1.DataSource;
            mdc_spm_Determination_1 sefi_1 = new mdc_spm_Determination_1();
            //sefi_1.ID = sefi_1_s.Count + 1;
            sefi_1_s.Insert(sefi_1_s.Count, sefi_1);
            gv_Determination_1.DataSource = sefi_1_s;
            gv_Determination_1.DataBind();
        }
        #endregion
        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            List<mdc_spm_Determination_1> base_1_s = (List<mdc_spm_Determination_1>)this.gv_Determination_1.DataSource;
            IList<mdc_spm_Determination_1> base1 = (mdc_spm_Determination_1.process == TaskAPI.Process & mdc_spm_Determination_1.incident == TaskAPI.Incident).List<mdc_spm_Determination_1>();
            if (base_1_s.Count == 1)
            {

                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                return;

            }
            base_1_s.RemoveAt(gv_Determination_1.ChangedRowIndex == -1 || gv_Determination_1.ChangedRowIndex >= base_1_s.Count ? base_1_s.Count - 1 : gv_Determination_1.ChangedRowIndex);
            bool flag = false;
            foreach (mdc_spm_Determination_1 base_1 in base1)
            {

                foreach (mdc_spm_Determination_1 base_2 in base_1_s)
                {
                    if (base_2.ID == base_1.ID)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    Storage.Delete(base_1);
                }
                flag = false;
            }
            gv_Determination_1.DataSource = base_1_s;
            gv_Determination_1.DataBind();

        }
        #endregion
        #region 方法 InitControls(初始化)
        public void InitControls()
        {
            gv_Determination_1.Key = Key;
            if (TaskAPI.IsInitiate)
            {
                List<mdc_spm_Determination_1> mdc_spm_Determination1 = new List<mdc_spm_Determination_1>();
                for (int i = 0; i < 5; i++)
                {
                    mdc_spm_Determination1.Add(new mdc_spm_Determination_1());
                    //mdc_spm_Determination1[i].No = i + 1;
                }
                gv_Determination_1.DataSource = mdc_spm_Determination1;
            }
            else
            {
                IList<mdc_spm_Determination_1> mdc_spm_Determination1 = (mdc_spm_Determination_1.process == TaskAPI.Process & mdc_spm_Determination_1.incident == TaskAPI.Incident & mdc_spm_Determination_1.status == 1).List<mdc_spm_Determination_1>();
                if (mdc_spm_Determination1.Count == 0)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        mdc_spm_Determination1.Add(new mdc_spm_Determination_1());
                       // mdc_spm_Determination1[i].No = i + 1;
                    }
                }
                 gv_Determination_1.DataSource = mdc_spm_Determination1;
            }
        }
        #endregion

        #region 方法 gv_Determination_1 数据取得
        protected void gv_Determination_1_DataBinding(object sender, EventArgs e)
        {
            //if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit && gv_CCC_1.ChangedColumnIndex == 9)
             
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!(Status.IsUsed && TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit))
            {
                return new List<string>();
            }

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            int i = 1;
            foreach (mdc_spm_Determination_1 ccc in (List<mdc_spm_Determination_1>)this.gv_Determination_1.DataSource)
            {
                if (string.IsNullOrEmpty(ccc.Brand) && string.IsNullOrEmpty(ccc.Brand_Description) && string.IsNullOrEmpty(ccc.Customer_Number_1) && string.IsNullOrEmpty(ccc.Function_1) && string.IsNullOrEmpty(ccc.Material_Description) && string.IsNullOrEmpty(ccc.Material_Group) && string.IsNullOrEmpty(ccc.Material_Number) && string.IsNullOrEmpty(ccc.Old_Material) && string.IsNullOrEmpty(ccc.Plant) && string.IsNullOrEmpty(ccc.Postal_Code) && string.IsNullOrEmpty(ccc.Sub_brand) && string.IsNullOrEmpty(ccc.Sub_brand_Description) && string.IsNullOrEmpty(ccc.Technology) && string.IsNullOrEmpty(ccc.Transportation_zone) && string.IsNullOrEmpty(ccc.Volume_rebate_group))
                {
                    continue;
                }

                if ((Page as PMDC).Actions.SelectedItem.Text == "1, Creation")
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, ccc.Plant, "&nbsp;&nbsp;  表单 行" + i + ": Plant为必填"));
                }
             
                i++;
            }

            if (i == 1)
            {
                t_Attachment list = (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == TaskAPI.Incident).ListFirst<t_Attachment>();
                if (list == null)
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, list, "&nbsp;&nbsp;" + "表单不能为空或者必须上传附件!"));
                }

            }
            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                
            }

            return vald_s;
        }
        #endregion
    }
}
 